import {
  CanActivate,
  ExecutionContext,
  Injectable,
  ForbiddenException,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { User } from '../user/user.entity';

@Injectable()
export class AdminGuard implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest();
    const user: User = request.user;

    // 检查用户是否存在以及是否有管理员权限
    console.log(user);
    if (!user || !user.isAdmin) {
      throw new ForbiddenException('没有管理员权限');
    }

    return true;
  }
}
